package com.ymatou.search;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import android.os.RemoteException;

import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.ymatou.run.UiDeviceUtil;
import com.ymatou.run.UiSelectorUtil;
import com.ymatou.util.Logger;

@SuppressWarnings("all")
public class SearchMainPageTest extends UiAutomatorTestCase{
	
	private static final String TAG = SearchMainPageTest.class.getSimpleName();
	private static UiDeviceUtil uiDevice = new UiDeviceUtil();
	private List<UiObject> lately_words;
	private List<UiObject> hotSuggest_words;
	
	public void testSearchMainPage() throws IOException, UiObjectNotFoundException, RemoteException, InterruptedException {
	    this.enterSearchPage("com.ymatou.shop:id/tv_fragment_home_search");
	    //this.checkSearchPage();
	    
	    //输入关键词并搜索
	    SeachEnterWordsTest.testSeachEnterWords();
	    
	    //搜索结果页验证
	    new SearchResultPageTest().testSearchResultPage();
    }
	
	public void checkSearchPage() throws UiObjectNotFoundException {
		//uiDevice.takeScreenSnapShot();
	    UiCollection collection = UiSelectorUtil.getParentCollection("android.widget.FrameLayout");
	    int count = UiSelectorUtil.getChildNumByCollection(collection, "android.widget.TextView");
	    UiSelector childSelector = new UiSelector().className("android.widget.TextView");
	    
		ArrayList words = new ArrayList<UiObject>();
		
		int lately_search = 0;
		int hot_suggest = 0;
	    
	    for (int i = 0; i < count; i++) {
	    	UiObject object = collection.getChildByInstance(childSelector, i);
	    	
	    	if (object.getText().equalsIgnoreCase("搜索"))
	    		continue;
	    	
	    	if (object.getText().equalsIgnoreCase("最近搜索"))
	    		lately_search = i;
	    	
	    	if (object.getText().equalsIgnoreCase("热门推荐"))
	    		hot_suggest = i;
	    	
	    	words.add(object);
	    }

	    this.setLately_words(words.subList(lately_search, hot_suggest - 1));
	    this.setHotSuggest_words(words.subList(hot_suggest, words.size()));
	     
	    SearchReturnTest.testClickSearchReturn();
	    Logger.log(TAG, "搜索主页返回");
	    
	    this.enterSearchPage("com.ymatou.shop:id/tv_fragment_home_search");
	    Logger.log(TAG, "重新进入搜索主页面");
	    
	    for (int i = 0; i < 3; i++) {
	    	SearchHotSuggestTest searchHotSuggestTest = new SearchHotSuggestTest(this.getHotSuggestWords());
		    searchHotSuggestTest.testClickHotSuggest();
        	SearchResultPageTest.testSearchResultPageExists();
		    SearchReturnTest.testClickSearchReturn();
	    }
	    
	    SearchLatelyWordsTest searchLatelyWordsTest = new SearchLatelyWordsTest(this.getLatelyWords());
	    searchLatelyWordsTest.testClickLatelywords();
    	Logger.log(TAG, "点击最近搜索词");
    	SearchResultPageTest.testSearchResultPageExists();
	    SearchReturnTest.testClickSearchReturn();
	}
	
	public void enterSearchPage(String resourceId) throws UiObjectNotFoundException {
		UiSelector fragment_home_search_s = new UiSelector().resourceId(resourceId);
	    UiObject fragment_home_search_o = new UiObject(fragment_home_search_s);
	    
	    Logger.log(TAG, fragment_home_search_o.getText());
	    fragment_home_search_o.click();
	    Logger.log(TAG, "进入搜索主页面");
	    
	    UiSelector searchPage_hotSuggest_s = new UiSelector().textStartsWith("热门推荐");
	    UiObject searchPage_hotSuggest_o = new UiObject(searchPage_hotSuggest_s);
	    
	    //收起弹出的键盘
	    searchPage_hotSuggest_o.waitForExists(3000);
	    if (searchPage_hotSuggest_o.exists()) {
	    	uiDevice.getUiDeviceInstance().pressBack();
	    }
	}
	
	public void setLately_words(List<UiObject> lately_words) throws UiObjectNotFoundException {
		
		for (UiObject o : lately_words)
			Logger.log(TAG, o.getText());
		
		this.lately_words = lately_words;
	}

	public void setHotSuggest_words(List<UiObject> hotSuggest_words) throws UiObjectNotFoundException {
		
		for (UiObject o : hotSuggest_words)
			Logger.log(TAG, o.getText());
		
		this.hotSuggest_words = hotSuggest_words;
	}
	
	public List<UiObject> getLatelyWords() {
		return this.lately_words;
	}
	
	public List<UiObject> getHotSuggestWords() {
		return this.hotSuggest_words;
	}
}
